//
//  BeeMemberController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/25.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "BeeMemberController.h"
#import "BeeMemberCell.h"
#import "UserObj.h"
#import "BeeGroupListObj.h"

@interface BeeMemberController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *listArray;
    UITableView *mainTable;
    UIView *navBar;
    BeeGroupListObj *beeObj;
    UIView *header;
}
@end

@implementation BeeMemberController

-(instancetype)initWithBeeObj:(BeeGroupListObj *)obj
{
    self = [super init];
    
    if (self) {
        beeObj = obj;
    }
    
    return self;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    listArray = [[NSMutableArray alloc] init];
    
    self.titleLabel.text = NSLocalizedString(@"bGroupManager", nil);
    self.largeTitle.hidden = YES;
    [self buildView];
    
    [self bringTitleToFront];
    [self.view bringSubviewToFront:navBar];
    
    [LoadingHelper showLoadingOnView:self.view];
    [self getMemberList];
    
}

-(void)buildView
{
    navBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, NavBarHeight)];
    navBar.backgroundColor = FBEE_COLOR_BACKGROUND;
    [self.view addSubview:navBar];
    
    UIButton *backNav =  [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 60, 44));
        make.addImage([UIImage imageNamed:@"nav_back"],UIControlStateNormal);
        make.addTarget(self,@selector(navback));
        make.imageEdgeInsets = UIEdgeInsetsMake(13.5, 16, 13.5, 27);
        make.tag = 666;
        [navBar addSubview:make];
    }];
    
    header = [[LargeTitle alloc] initWithTitle:NSLocalizedString(@"bGroupManager", nil)];
    mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT) style:UITableViewStyleGrouped];
    mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    mainTable.alwaysBounceVertical = YES;
    mainTable.backgroundColor = [UIColor clearColor];
    mainTable.tableHeaderView = header;
    mainTable.delegate = self;
    mainTable.dataSource = self;
    [self.view addSubview:mainTable];
    
    __weak id sel = self;
}

#pragma - mark scrollView

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //导航渐变
    if (mainTable.top >= 0) {
        
        float alphaY = scrollView.contentOffset.y;
        
        float alpha = alphaY / (scrollView.bounds.size.height/5.0f);
        alpha = alpha<0?0:alpha;
        alpha = alpha>1?1:alpha;
        [self.navigationView setAlpha:alpha];
        [self.view bringSubviewToFront:self.navigationView];
        
        if (self.navigationView.alpha == 1) {
            navBar.hidden = YES;
        }
        else
        {
            navBar.hidden = NO;
        }
        
    }
}

#pragma mark - TableView Cell

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return listArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"CELLID";
    
    UserObj *obj = [listArray objectAtIndex:indexPath.row];
    
    BeeMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        cell = [[BeeMemberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    cell.userObj = obj;
    
    return cell;
}

-(void)reLoadMainTable
{
    [mainTable reloadData];
}


#pragma mark - request

-(void)getMemberList
{
    NSString *url = [NSString stringWithFormat:@"%@?gid=%@&uid=%@",BeeGroupMember_URL,beeObj.groupId,[UserInfoManager sharedManager].userId];
    
    [self get:url code:BeeGroupMember_URL_Code];
}

#pragma mark -  response
-(void)requestSuccess:(id)data code:(NSInteger)code
{
    [LoadingHelper removeLoadingOnView:self.view];
    
    [EmptyViewHelper removeEmptyFromView:mainTable];
    
    [listArray removeAllObjects];
    NSArray *dataArray = [data objectForKey:@"memberList"];
    
    if (dataArray.count == 0) {
        
        if (listArray.count == 0) {
            UIView *emptyView = [EmptyViewHelper showEmptyOnView:mainTable imageName:@"common_empty" text:@"no data"];
            emptyView.height = 220;
            emptyView.top = self.largeTitle.bottom;
        }
        
    }
    
    for (NSDictionary *dataDict in dataArray)
    {
        UserObj *obj = [[UserObj alloc] init];
        [obj setObjByDict:dataDict];
        [listArray addObject:obj];
    }
    
    [mainTable reloadData];
}

-(void)requestLogicError:(id)data code:(NSInteger)code
{
    [super requestLogicError:data code:code];
}

-(void)requestFailure:(NSError *)error code:(NSInteger)code
{
    
}




@end
